package com.kkm.dao.Imp;

import com.kkm.dao.GoodsDao;
import com.kkm.pojo.Goods;
import com.kkm.pojo.Order;
import com.kkm.pojo.SongList;
import com.kkm.util.JdbcUtils_Druid;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;

public class GoodsDaoImp implements GoodsDao {
    private JdbcTemplate template = new JdbcTemplate(JdbcUtils_Druid.getDataSource());
    @Override
    public List<Goods> showGoods(int index, int rows) {
        String sql="SELECT * FROM goods LIMIT ?,?";
        return template.query(sql, new BeanPropertyRowMapper<>(Goods.class), index, rows);
    }

    @Override
    public int checkGoodsCount() {
        String sql = "select count(*)  from `goods` ";
        return template.queryForObject(sql, int.class);
    }

    @Override
    public int checkGoodsNum(Map<String, String[]> condition) {
        try {

            String sql = "select count(*) from `goods`  where 1=1";
            //解析map  1.对sql拼接
            StringBuffer sb = new StringBuffer(sql);
            //2.获取键值，name address，email
            Set<String> keySet = condition.keySet();
            //3.定义参数集合
            List<Object> params = new ArrayList<Object>();
            //遍历看那个不为空拼接到sql语句中
            //key：name，address，email
            for(String key:keySet){
                //排除分页条件参数
                if ("currentPage".equals(key)||"rows".equals(key)||"method".equals(key)){
                    continue;
                }
                String value=condition.get(key)[0];//获取值
                if(value!=null||"".equals(value)){
                    //有值
                    sb.append(" and "+key+" like ? ");
                    params.add("%%"+value+"%");
                }
            }
            System.out.println("1111"+sb.toString());
            return template.queryForObject(sb.toString(), int.class, params.toArray());
        } catch (Exception e) {
            return 0;
        }
    }

    @Override
    public List<Goods> checkGoods(Map<String, String[]> condition, int index, int rows) {
        try{
            String sql="select * from goods where 1=1 ";
            //解析map  1.对sql拼接
            StringBuffer sb=new StringBuffer(sql);
            //2.获取键值，name address，email
            Set<String> keySet=condition.keySet();
            //3.定义参数集合
            List<Object> params=new ArrayList<Object>();
            //遍历看那个不为空拼接到sql语句中
            //key：name，address，email
            for(String key:keySet){
                //排除分页条件参数
                if ("currentPage".equals(key)||"rows".equals(key)||"method".equals(key)){
                    continue;
                }
                String value=condition.get(key)[0];//获取值
                if(value!=null||"".equals(value)){
                    //有值
                    sb.append(" and "+key+" like ? ");
                    params.add("%%"+value+"%");
                }
            }
            sb.append(" limit ?,? ");
            params.add(index);
            params.add(rows);
            sql=sb.toString();
            return template.query(sql,new BeanPropertyRowMapper<Goods>(Goods.class),params.toArray());
        }catch (Exception e){
            return null;
        }
    }

    @Override
    public void deleteGoods(int gid) {
        String  sql="delete from goods where id=? ";
        template.update(sql,gid);
    }

    @Override
    public Goods goodsById(int gid) {
        String sql="select * from goods where id=?";
        return template.queryForObject(sql,new BeanPropertyRowMapper<>(Goods.class),gid);
    }

    @Override
    public void modifyGoods( double price,int number, int gid) {
        String sql="UPDATE `goods` SET  number = ? , price = ? WHERE id=?";
        template.update(sql,number,price,gid);
    }

    @Override
    public void addGoods(String gname, double price, int number, String gtype) {
        String sql="insert into goods (gname,price,number,gtype) values(?,?,?,?)";
        System.out.println(gname+" "+price+" "+number+" "+gtype);
        template.update(sql,gname,price,number,gtype);
    }
}
